<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>会员管理</el-breadcrumb-item>
				<el-breadcrumb-item>编辑会员组</el-breadcrumb-item>
			</el-breadcrumb>
		</div>

		<div class="content-box">
			<el-tabs v-model="activeName" type="card">
				<el-tab-pane label="基本信息" name="info">
					<div class="tab-content">
						<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
							<el-form-item prop="title" label="会员组名">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="minExp" label="最小经验值">
								<el-input v-model="model.minExp" placeholder="必填，只能为正整数"></el-input>
							</el-form-item>
							<el-form-item prop="maxExp" label="最大经验值">
								<el-input v-model="model.maxExp" placeholder="必填，只能为正整数"></el-input>
							</el-form-item>
							<el-form-item prop="amount" label="预存款">
								<el-input v-model="model.amount" placeholder="必填，只能为正整数">
									<template slot="append">元</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="discount" label="购物折扣">
								<el-slider v-model="model.discount" :min="1" show-input :max="100"></el-slider>
							</el-form-item>
							<el-form-item label="自动升级">
								<el-switch :active-value="1" :inactive-value="0" v-model="model.isUpgrade"></el-switch>
							</el-form-item>
							<el-form-item label="注册默认组">
								<el-switch :active-value="1" :inactive-value="0" v-model="model.isDefault"></el-switch>
							</el-form-item>
							<el-form-item label="创建时间" v-if="model.id>0">
								{{model.addBy}} {{model.addTime}}
							</el-form-item>
						</el-form>
					</div>
				</el-tab-pane>
			</el-tabs>
		</div>
		
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" :loading="loading" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	export default {
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				loading: false,
				model: {
					id: 0,
					title: null,
					minExp: null,
					maxExp: null,
					amount: null,
					discount: 100,
					isUpgrade: 1,
					isDefault: 0,
					addBy: null,
					addTime: null,
				},
				rules: {
					title: [
						{ required: true, message: '请输入用户组名称', trigger: 'blur' }
					],
					minExp: [
						{ required: true, message: '请输入升级最小经验值', trigger: 'blur' },
						{ pattern: /^[+]{0,1}(\d+)$/, message: '经验值只能为正整数', trigger: 'blur' }
					],
					maxExp: [
						{ required: true, message: '请输入升级最大经验值', trigger: 'blur' },
						{ pattern: /^[+]{0,1}(\d+)$/, message: '经验值只能为正整数', trigger: 'blur' }
					],
					amount: [
						{ required: true, message: '请输入预存金额(元)', trigger: 'blur' },
						{ pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确金额，可保留两位小数', trigger: 'blur' }
					],
					discount: [
						{ required: true, message: '请选择购物折扣', trigger: 'change' }
					]
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this; //当前页面
				if (_this.id) {
					_this.$api.request({
						url: '/admin/member/group/' + _this.id,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
					});
				}
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: '/admin/member/group/' + _this.model.id,
							data: _this.model,
							successMsg: '修改会员组已成功',
							beforeSend() {
								_this.loading = true
							},
							success(res) {
								//可跳转加列表页
								_this.$common.linkUrl('/member/group/list');
							},
							complete() {
								_this.loading = false
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/member/group',
							data: _this.model,
							successMsg: '新增会员组已成功',
							beforeSend() {
								_this.loading = true
							},
							success(res) {
								//重置表单
								_this.$refs['editForm'].resetFields();
							},
							complete() {
								_this.loading = false
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>